<?php
include("_session.php");
if(count($_POST)>0)
{
	$order_no = $_POST['ORDNO'];
	$ordcamph = $_POST["ORDCAMP"];		
	$datetime = explode(" ", $_POST['ORDDATE']);
	$orddate = wrapperDate($datetime[0]);
	$ordtime = str_replace(":","",$datetime[1]);
	$curcamp = getCurCamp($conn);
	$dist = str_pad($_POST["DIST"],4,0,STR_PAD_LEFT);
	$mslno = str_pad($_POST["MSLNO"],5,0,STR_PAD_LEFT);
	$chkdgt = $_POST["CHKDGT"];
	$memberno = $_POST["MEMBERNO"];
	$membername = $_POST["MSLNAME"];
	$totalamount = 0;	

	$rows = $_SESSION['ORDER_DETAIL'];
	$i=0;
	if (HeaderExists($conn,$order_no,$ordcamph,$dist,$mslno,$chkdgt))
	{
		$respond_message = "DUPLICATE";
		$error_message = "DUPLICATE! ".$order_no."/".$ordcamph."(".$dist."-".$mslno."-".$chkdgt.")";
	}
	else
	{
	    mysql_query("BEGIN");        
		foreach($rows AS $rowNum => $row)
		{
	        $LISTNO = ++$i;
	        $BILLCODE = $row['BILLCODE'];
	        $BILLDESC = $row['BILLDESC'];
	        $QTY = str_replace(",","", $row['QTY']);        
	        $PRICE = str_replace(",","", $row['PRICE']);
	        $AMOUNT = str_replace(",","", $row['AMOUNT']);
	        $ORDCAMPD = $row['ORDCAMPD'];
	        $REMARK = 'N';
	        $BRAND = 'N';
	        $DISCOUNT = 'NULL';
	        $SPCFLG = 'N';
	        $DISCFLG = 'N';
	        $INCTFLG = 'N';
	        $FREEFLG = 'N';
	        $BILLFLG = 'N';
	        $DWNFLG = 'N';
	        $EXPFLG = 'N';
	        $FLAG1 = 'N';
	        $DELFLAG = 'N';
	        
	        $sql = "INSERT INTO order_detail (
	        	ORDER_NO ,
				ORDCAMPH ,
				ORDDATE ,
				ORDTIME ,
				CURCAMP ,
				DIST ,
				MSLNO ,
				CHKDGT ,
				LISTNO ,
				BILLCODE ,
				BILLDESC ,
				QTY ,
				PRICE ,
				AMOUNT ,
				REMARK ,
				BRAND ,
				DISCOUNT ,
				SPCFLG ,
				DISCFLG ,
				INCTFLG ,
				FREEFLG ,
				BILLFLAG ,
				DWNFLAG ,
				EXPFLAG ,
				FLAG1 ,
				DELFLAG ,
				ORDCAMPD
	        ) VALUES (
	        	$order_no,  
	        	$ordcamph,  
	        	$orddate,  
	        	'".$ordtime."',  
	        	$curcamp,  
	        	'".$dist."',  
	        	$mslno,  
	        	$chkdgt,  
	        	$LISTNO,  
	        	'".$BILLCODE."',  
	        	'".$BILLDESC."',  
	        	$QTY,  
	        	$PRICE,  
	        	$AMOUNT,  
	        	'".$REMARK."', 
	        	'".$BRAND."' ,  
	        	$DISCOUNT,  
	        	'".$SPCFLG."',  
	        	'".$DISCFLG."',  
	        	'".$INCTFLG."',  
	        	'".$FREEFLG."',  
	        	'".$BILLFLG."',  
	        	'".$DWNFLG."',  
	        	'".$EXPFLG."',  
	        	'".$FLAG1."',  
	        	'".$DELFLAG."',  
	        	$ORDCAMPD
	        )";
	        //echo $sql;
	    	$result = mysql_query($sql, $conn);
	    	if (!$result)
	    	{
	    		$log = date("H:i:s")."-".$_SESSION["RACCOUNT_ID"];
	    		error_log($log.": ".mysql_error($conn).PHP_EOL, 3, "D:/AppServ/www/tosorderbackup/log/err".date("Ymd").".log");	
				mysql_query("ROLLBACK");	    		
	    		$respond_message = "ERROR";
				$error_message = "ROLLBACK! D".$order_no."/".$ordcamph."(".$dist."-".$mslno."-".$chkdgt.")";
				break;
			}
			$totalamount = $totalamount + $AMOUNT;
		}
		
		if ($result)
		{
			$sql = "INSERT INTO  order_header (
				ORDER_NO ,
				ORDCAMP ,
				ORDDATE ,
				ORDTIME ,
				CURCAMP ,
				DIST ,
				MSLNO ,
				CHKDGT ,
				NAME ,
				ITEMS ,
				TOTAL_AMOUNT ,
				AMOUNT_1 ,
				AMOUNT_2 ,
				AMOUNT_3 ,
				DISCOUNT_1 ,
				DISCOUNT_2 ,
				DISCOUNT_3 ,
				BILLDATE ,
				SHIPDATE ,
				DLVDATE ,
				DWNDATE ,
				DWNFLAG ,
				EXPFLAG ,
				MAIL_CONFIRM ,
				UPDDATE ,
				UPDTIME ,
				DP_DOWNLOAD ,
				WEBSITE_ID ,
				DELFLAG ,
				BROWSER ,
				ACCOUNT_ID
			) VALUES (
	        	$order_no,  
	        	$ordcamph,  
	        	$orddate,  
	        	'".$ordtime."',  
	        	$curcamp,  
	        	'".$dist."',  
	        	$mslno,  
	        	$chkdgt,
	        	'".$membername."',
	        	$i,
	        	$totalamount,
				0,
				0,
				0,
				0,
				0,
				0,
				0,
				0,
				0,
				0,
				'N',
				'N',
				'N',
				'".date("Ymd")."',
				'".date("His")."',
				'',
				'',
				'N',
				'',
				'".$_SESSION["RACCOUNT_ID"]."'		
			)";
			//echo $sql;
			$result = mysql_query($sql, $conn);
	    	if (!$result)
	    	{
	    		$log = date("H:i:s")."-".$_SESSION["RACCOUNT_ID"];
	    		error_log($log.": ".mysql_error($conn).PHP_EOL, 3, "D:/AppServ/www/tosorderbackup/log/err".date("Ymd").".log");	
				mysql_query("ROLLBACK");	    		
	    		$respond_message = "ERROR";
				$error_message = "ROLLBACK! H".$order_no."/".$ordcamph."(".$dist."-".$mslno."-".$chkdgt.")";
				//echo mysql_errno($conn) . ": " . mysql_error($conn). "\n";
			}
			else
			{
				mysql_query("COMMIT");
				$respond_message = "SUCCESSFULL";
			}
		}
		else
		{
			$sql = "DELETE FROM order_detail 
				WHERE 
					ORDER_NO = ".$order_no." AND 
					ORDCAMPH = ".$ordcamph." AND 
					DIST = '".$dist."' AND
					MSLNO = ".$mslno." AND
					CHKDGT = ".$chkdgt;
			$result = mysql_query($sql, $conn);
			if (!$result)
			{
    			$log = date("H:i:s")."-".$_SESSION["RACCOUNT_ID"];
    			error_log($log.": ".mysql_error($conn).PHP_EOL, 3, "D:/AppServ/www/tosorderbackup/log/err".date("Ymd").".log");	
			}
		}		
	}
	header("Location:resultnew.php?respond_message=".$respond_message."&dist=".$dist."&mslno=".$mslno."&chkdgt=".$chkdgt."&ordcamp=".$ordcamph."&order_no=".$order_no);
}

function HeaderExists($conn,$order_no,$ordcamph,$dist,$mslno,$chkdgt)
{
	$sql = "SELECT count(*) as numrow 
	FROM order_header oh 
		WHERE 
		oh.DIST ='".$dist."'  
	AND oh.MSLNO = ".$mslno." 
	AND oh.CHKDGT = ".$chkdgt." 
	AND oh.ORDCAMP = ".$ordcamph." 
	AND oh.ORDER_NO = ".$order_no;
	//echo $sql;
	$raw = mysql_query($sql, $conn);
	$row = mysql_fetch_array($raw, MYSQL_ASSOC);
	if ($row["numrow"] > 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

include("_close.php");
?>